--
-- Author: ping.wang
-- Date: 2018-07-10 17:26:38
--
-- AuctionStorageItemRender
--

local string_format = string.format
local IconRender = require("app.widget.renders.IconRender")
local ClassRef = app.mvc.createRenderClass(...)
local TimeUtil_secondsToTime = TimeUtil.secondsToTime

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AuctionStorageItem))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )

    self._normalPanel = self._ui.Panel_1 --普通商品的panel
    self._bidPanel = self._ui.Panel_2 --竞价商品panel

    self._pulloffBtn = self._ui.Button_1 --下架按钮 出过价的竞价商品是不可以下架的
    self._pulloffBtn:addClickEventListener(function()
        if self._data and self._data[2] then
            executeFunc(self._onClick, "pullOffItem", self._data[2])
        end
    end)

    self._itemIcon = IconRender.create(self._ui.icon)
    self._itemIcon:setTipEnable(true)

    self._normalItemName  = self._normalPanel:findChild("name") --普通显示道具名
    self._normalItemNum = self._normalPanel:findChild("num1") --数量
    self._normalItemPrice = self._normalPanel:findChild("num_2/num") --售价
    self._normalGoldIcon = self._normalPanel:findChild("num_2/gold") --普通商品的货币icon
    self._normalOfferPanel = self._normalPanel:findChild("Panel_bar_1") --显示上架的panel 显示多少秒后上架
    self._normalOfferBar = self._normalPanel:findChild("Panel_bar_1/LoadingBar") --普通显示的上架panel时间进度条
    self._normalOfferText = self._normalPanel:findChild("Panel_bar_1/num") --普通显示的上架panel提示 ： XX:XX:XX后上架
    self._normalPullPanel = self._normalPanel:findChild("Panel_bar_2") --普通显示的下架panel
    self._normalPullBar = self._normalPanel:findChild("Panel_bar_2/LoadingBar") --普通显示的下架的进度条
    self._normalPullText = self._normalPanel:findChild("Panel_bar_2/num") --普通显示的下架的提示 XX:XX:XX后下架



    local coinId = tonumber(GD:getSystemPar( "auctionNormalGoodsLootType",99))
    local coinConfig = GD:getCoinConfig(coinId)
    local iconPath = string.format("%s%s.png",Res.coinIconPath,coinConfig.bigicon)
    self._normalGoldIcon:loadTexture(iconPath)


    self._bidItemName  = self._bidPanel:findChild("name") --竞价显示道具名
    self._bidFixedPrice = self._bidPanel:findChild("num_fixed/num") --竞价商品的一口价
    self._bidFixedGoldIcon = self._bidPanel:findChild("num_fixed/gold") --显示金币的ICON
    self._bidItemCurrentPrice = self._bidPanel:findChild("num_1/num") --当前别人的最高出价
    self._bidItemCurrentGoldIcon = self._bidPanel:findChild("num_1/gold") --当前别人的最高出价的货币ICON
    self._bidOfferPanel = self._bidPanel:findChild("Panel_bar_1") --显示上架的panel 显示多少秒后上架
    self._bidOfferBar = self._bidPanel:findChild("Panel_bar_1/LoadingBar") --竞价显示的上架panel时间进度条
    self._bidOfferText = self._bidPanel:findChild("Panel_bar_1/num") --竞价显示的上架panel提示 ： XX:XX:XX后上架
    self._bidPullPanel = self._bidPanel:findChild("Panel_bar_2") --竞价显示的下架panel
    self._bidPullBar = self._bidPanel:findChild("Panel_bar_2/LoadingBar") --竞价显示的下架的进度条
    self._bidPullText = self._bidPanel:findChild("Panel_bar_2/num") --竞价显示的下架的提示  XX:XX:XX后下架


    
    local bidCoinId = 99
    local publisher = app:getInnerPublisher()
    if publisher == Enums.Publisher.tencent then
        bidCoinId = tonumber(GD:getSystemPar( "auctionBiddingGoodsLootType_tencent",99))
    else
        bidCoinId = tonumber(GD:getSystemPar( "auctionBiddingGoodsLootType",115))
    end
    
    local bidCoinConfig = GD:getCoinConfig(bidCoinId)
    local bidIconPath = string.format("%s%s.png",Res.coinIconPath,bidCoinConfig.bigicon)
    self._bidFixedGoldIcon:loadTexture(bidIconPath)
    self._bidItemCurrentGoldIcon:loadTexture(bidIconPath)

    self:refresh()
end

function ClassRef:refresh()
     if self._data and self._data[2] then
        self:setVisible( true )
        local goodData = self._data[2]
        --dump(goodData)
        local itemData = goodData:getItemData()
        self._itemIcon:setData(itemData)
        if itemData:getAcType() == Enums.AuctionProductType.normal then
            self:_refreshNormalItem(goodData)
        else
            self:_refreshBidItem(goodData)
        end

    else
        self:setVisible( false )
        self:stopAllActions()
    end
end

--根据数据刷新普通商品
function ClassRef:_refreshNormalItem(goodData)
    self._normalPanel:setVisible(true)
    self._bidPanel:setVisible(false)
    self._pulloffBtn:setVisible(true)
    local itemData = goodData:getItemData()
    self._normalItemName:setString(itemData:getName(true))
    self._normalItemNum:setString(goodData:getCount())
    self._normalItemPrice:setString(goodData:getSellPrice())

    self:stopAllActions()
    self:_refreshNormalTimeProgressBar(goodData)
    self:schedule(function() self:_refreshNormalTimeProgressBar(goodData) end,1)
end


--刷新普通商品显示的进度条
function ClassRef:_refreshNormalTimeProgressBar(goodData)
    local outWaitTime = goodData:getOutWaitTime()
    local outSaleDurationTime = goodData:getOutSaleDurationTime()

    if outWaitTime >= 0 then
        self._normalPullPanel:setVisible(false)

        self._normalOfferPanel:setVisible(true)
        self._normalOfferText:setVisible(true)
        self._normalOfferBar:setVisible(true)
        local timeStr = TimeUtil_secondsToTime(outWaitTime)
        self._normalOfferText:setString(string_format(L("text_10800027"),timeStr))
        self._normalOfferBar:setPercent(goodData:getLastWaitTimePecent(outWaitTime))

    elseif outSaleDurationTime >= 0 then
        self._normalOfferPanel:setVisible(false)

        self._normalPullPanel:setVisible(true)
        self._normalPullBar:setVisible(true)
        self._normalPullText:setVisible(true)

        local timeStr = TimeUtil_secondsToTime(outSaleDurationTime)
        self._normalPullText:setString(string_format(L("text_12200013"),timeStr))
        self._normalPullBar:setPercent(goodData:getLastOnShelvesTimePecent(outSaleDurationTime))
    else
        --该商品需要下架 因为已经超过上架的时间了
        self:stopAllActions()
        executeFunc(self._onClick, "localOffShelves",goodData:getProductUUId())
    end
end

function ClassRef:_refreshBidItem(goodData)
    self._normalPanel:setVisible(false)
    self._bidPanel:setVisible(true)
    --出过价的竞价商品是不可以下架的
    local hasBiding = goodData:hasBiding() --竞价商品当前的竞价
    if hasBiding then
        self._pulloffBtn:setVisible(false)
    else
        self._pulloffBtn:setVisible(true)
    end

    local itemData = goodData:getItemData()
    self._bidItemName:setString(itemData:getName(true))
    self._bidFixedPrice:setString(string_format(L("auction_donePrice"),goodData:getFixedPrice()))
    self:_adjustIconPosX(self._bidFixedGoldIcon,self._bidFixedPrice)
    self._bidItemCurrentPrice:setString(tostring(goodData:getBidingPrice()))

    self:stopAllActions()
    self:_refreshBidTimeProgressBar(goodData)
    self:schedule(function() self:_refreshBidTimeProgressBar(goodData) end,1)

end

--基于文本调整ICON的位置
function ClassRef:_adjustIconPosX(icon,text)
    local fixedGoldenPosX = text:getContentSize().width + icon:getContentSize().width * 0.6
    icon:setPositionX(fixedGoldenPosX)
end

--刷新竞价商品显示的进度条
function ClassRef:_refreshBidTimeProgressBar(goodData)
    local outWaitTime = goodData:getOutWaitTime()
    local outSaleDurationTime = goodData:getOutSaleDurationTime()

    if outWaitTime >= 0 then
        self._bidPullPanel:setVisible(false)

        self._bidOfferPanel:setVisible(true)
        self._bidOfferText:setVisible(true)
        self._bidOfferBar:setVisible(true)
        local timeStr = TimeUtil_secondsToTime(outWaitTime)
        self._bidOfferText:setString(string_format(L("text_10800027"),timeStr))
        self._bidOfferBar:setPercent(goodData:getLastWaitTimePecent(outWaitTime))

    elseif outSaleDurationTime >= 0 then
        self._bidOfferPanel:setVisible(false)

        self._bidPullPanel:setVisible(true)
        self._bidPullBar:setVisible(true)
        self._bidPullText:setVisible(true)

        local timeStr = TimeUtil_secondsToTime(outSaleDurationTime)
        self._bidPullText:setString(string_format(L("text_12200013"),timeStr))
        self._bidPullBar:setPercent(goodData:getLastOnShelvesTimePecent(outSaleDurationTime))
    else
        --该商品需要下架 因为已经超过上架的时间了
        self:stopAllActions()
        executeFunc(self._onClick, "localOffShelves",goodData:getProductUUId())
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
